/*
 * Stock.java
 *
 * Created on 27 March 2007, 12:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;

/**
 * Entity class Stock
 *
 * @author Yuezhong Zhang
 */
@Entity(name="Stock")
@NamedQueries(
{ 
   @NamedQuery(
     name="getItemByWarehouse",
     query="SELECT s FROM Stock s WHERE s.warehouse = :warehouse"
   ), 
   @NamedQuery(
     name="getHoldingByWarehouse",
     query="SELECT c FROM Stock c WHERE c.warehouse = :warehouse AND c.catalogueId = :catalogueId"
   )
 }
)
public class Stock implements Serializable {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="StoIdSeq")
    @SequenceGenerator(name="StoIdSeq", sequenceName="sto_id_seq")
    private Integer id;
    
    /** Creates a new instance of Stock */
    public Stock() {
    }
    
    public Stock(StockInfo s) {
        setCatalogueId(s.getCatalogue_id());
        setHolding(s.getHolding());
        if (s.getId() != StockInfo.UNDEFINED)
            setId(s.getId());
        setWarehouse(s.getWarehouse());
    }
    
    public StockInfo getStockInfo() {
        return new StockInfo(getId(), getCatalogueId(), getWarehouse(), getHolding());
    }
    
    /**
     * Gets the id of this Stock.
     * @return the id
     */
    public Integer getId() {
        return this.id;
    }
    
    /**
     * Sets the id of this Stock to the specified value.
     * @param id the new id
     */
    public void setId(Integer id) {
        this.id = id;
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this Stock.  The result is
     * <code>true</code> if and only if the argument is not null and is a Stock object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Stock)) {
            return false;
        }
        Stock other = (Stock)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "usyd.comp5348.entity.Stock[id=" + id + "]";
    }
    
    /**
     * Holds value of property catalogueId.
     */
    @Column(name="catalogue_id",nullable =false)
    private int catalogueId;
    
    /**
     * Getter for property catalogueId.
     * @return Value of property catalogueId.
     */
    
    public int getCatalogueId() {
        return this.catalogueId;
    }
    
    /**
     * Setter for property catalogueId.
     * @param catalogueId New value of property catalogueId.
     */
    public void setCatalogueId(int catalogueId) {
        this.catalogueId = catalogueId;
    }
    
    /**
     * Holds value of property warehouse.
     */
    @Column(name="warehouse", length=16, nullable=false)
    private String warehouse;
    
    /**
     * Getter for property warehouse.
     * @return Value of property warehouse.
     */
    public String getWarehouse() {
        return this.warehouse;
    }
    
    /**
     * Setter for property warehouse.
     * @param warehouse New value of property warehouse.
     */
    public void setWarehouse(String warehouse) {
        this.warehouse = warehouse;
    }
    
    /**
     * Holds value of property holding.
     */
    @Column(name="holding", nullable=false)
    private int holding;
    
    /**
     * Getter for property holding.
     * @return Value of property holding.
     */
    public int getHolding() {
        return this.holding;
    }
    
    /**
     * Setter for property holding.
     * @param holding New value of property holding.
     */
    public void setHolding(int holding) {
        this.holding = holding;
    }
    
    
}
